<template>
    <div>
        <el-table :data="store.state.admin.userList" border style="width: 100%">
            <el-table-column prop="uid" label="用户编号" width="180" />
            <el-table-column prop="username" label="用户名称" width="180" />
            <el-table-column prop="rolename" label="所属角色" width="180" />
            <el-table-column label="状态">
                <template v-slot="a">
                    <el-tag type="success" v-if="a.row.status == 1">启用</el-tag>
                    <el-tag type="danger" v-if="a.row.status == 2">禁用</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template v-slot="a">
                    <el-button plain type="primary" @click="edit(a.row.uid)">编辑</el-button>
                    <el-button plain type="danger" @click="del(a.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination @current-change="change" background layout="prev, pager, next" :total="store.state.admin.userCount"
            :page-size='store.state.admin.pageData.size' :current-page="store.state.admin.pageData.page" />
    </div>
</template>
  
<script lang="ts" setup>
import mit from '../../common/myMitt';
import { useStore } from "vuex";
import { userdelete } from '../../request/api';
import { ElMessage } from "element-plus";
import { getCurrentInstance } from "vue";
import { isFragment } from 'element-plus/es/utils';

let getapp: any = getCurrentInstance();
let store = useStore();

store.dispatch('admin/getUserList');
store.dispatch('admin/getUserCount');

function change(e: any) {
    store.commit('admin/changePage', e);
    store.dispatch('admin/getUserList');
}

function del(id: any) {
    getapp.appContext.config.globalProperties.$del(() => {
        userdelete({ id }).then((res: any) => {
            if (res.code == 200) {
                ElMessage.success(res.msg);
                store.dispatch('admin/getUserCount');
                if (store.state.admin.userCount % store.state.admin.pageData.size != 1) {
                    store.dispatch('admin/getUserList');
                }
                return;
            }
            ElMessage.warning(res.msg);
        })
    })
}

function edit(uid: any) {
    mit.emit('getInfo', uid);
}
</script>
  
<style scoped>
.el-pagination {
    margin: 20px 0 0 0;
}
</style>